package com.thermal.thermal_inspection.service.impl;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.Map;
import com.thermal.thermal_inspection.entity.MapDetail;
import com.thermal.thermal_inspection.mapper.InspectionDataMapper;
import com.thermal.thermal_inspection.mapper.InspectionMapper;
import com.thermal.thermal_inspection.mapper.MapDetailMapper;
import com.thermal.thermal_inspection.mapper.MapMapper;
import com.thermal.thermal_inspection.service.MapService;
import com.thermal.thermal_inspection.vo.MapDetailVO;
import io.micrometer.observation.annotation.Observed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MapServiceImpl implements MapService {
    @Autowired
    MapMapper mapMapper;
    @Autowired
    MapDetailMapper mapDetailMapper;
    @Autowired
    InspectionDataMapper inspectionDataMapper;
    @Autowired
    InspectionMapper inspectionMapper;

    /**
     *查询已保存地图
     */
    @Override
    public List<Map> getMaps(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return mapMapper.getPageMaps(offset, pageSize);
    }

    /**
     * 获取地图总数量
     */
    @Override
    public int getTotal(){
        return mapMapper.getTotal();
    }

    /**
     * 判断地图是否存在
     */
    @Override
    public Map existMap(int mapId){
        return mapMapper.getMap(mapId);
    }

    /**
     * 查询地图所有点位
     */
    @Override
    public List<MapDetailVO> getAllMapDetail(int mapId){
        return mapDetailMapper.getAllMapDetail(mapId);
    }

    /**
     * 查询特定地图点位
     */
    @Override
    public MapDetailVO getOneMapDetail(int mapDetailId){
        return mapDetailMapper.getOneMapDetail(mapDetailId);
    }

    /**
     * 新增地图
     */
    @Override
    public int addMap(String mapName, String mapLongitude, String mapLatitude, int unitId, String mapDetailDesc, String mapNfcMsg){
        //先判断mapName是否存在，存在则不用新增地图，不存在则先新增地图
        if(mapMapper.getMapByMapName(mapName) == null){
            Timestamp mapDate = new Timestamp(System.currentTimeMillis());
            int rows1 = mapMapper.addMap(mapName, mapDate);
            if(rows1 == 0)
                return 0;
        }

        //获取mapId，然后直接新增点位
        int mapId = mapMapper.getMapIdByMapName(mapName);
        int rows2 = mapDetailMapper.addMapDetail(mapId, mapLongitude, mapLatitude, unitId, mapDetailDesc, mapNfcMsg);
        return rows2;
    }

    /**
     * 通过mapName获取地图
     */
    @Override
    public Map getMapByMapName(String mapName){
        return mapMapper.getMapByMapName(mapName);
    }

    /**
     * 地图添加点位
     */
    @Override
    public int addMapDetail(int mapId, String mapLongitude, String mapLatitude, int unitId, String mapDetailDesc, String mapNfcMsg){
        return mapDetailMapper.addMapDetail(mapId, mapLongitude, mapLatitude, unitId, mapDetailDesc, mapNfcMsg);
    }

    /**
     * 修改地图名称
     */
    @Override
    public int changeMapName(int mapId, String mapName){
        return mapMapper.changeMapName(mapId, mapName);
    }

    /**
     * 判断地图点位是否存在
     */
    @Override
    public boolean existMapDetail(int mapDetailId){
        return mapDetailMapper.getMapDetailById(mapDetailId) != null;
    }

    /**
     * 修改地图点位信息
     */
    @Override
    public int changeMapDetail(int mapDetailId, int mapId, String mapDetailDesc) {
        return mapDetailMapper.changeMapDetail(mapDetailId, mapId, mapDetailDesc);
    }

    /**
     * 删除地图
     */
    @Observed
    public int deleteMap(int mapId){
        //把inspection_data里面涉及相关点位的给删掉
        List<MapDetailVO> mapDetailList = mapDetailMapper.getAllMapDetail(mapId);
        System.out.println("mapDetailList:" + mapDetailList);
        for(MapDetailVO mapDetailVO: mapDetailList){
            System.out.println("MapDetailId:" + mapDetailVO.getMapDetailId());
            inspectionDataMapper.deleteMapDetail(mapDetailVO.getMapDetailId());
        }

        //把map表中的删除
        int rows1 = mapMapper.deleteMap(mapId);
        //把巡检地图中有mapId的设置为-1
        int rows2 = mapDetailMapper.deleteMap(mapId);

        //把inspection表里面的inspection_map_id设置成-1
        int rows3 = inspectionMapper.deleteMap(mapId);

        return rows1;
    }

    /**
     * 删除地图点位
     */
    @Override
    public int deleteMapDetail(int mapDetailId){
        inspectionDataMapper.deleteMapDetail(mapDetailId);
        return mapDetailMapper.deleteMapDetail(mapDetailId);
    }

    /**
     * 把N地图点位的NFC信息设置为空
     * @param nfcMsg
     * @return Integer
     */
    @Override
    public Integer updateNFCToEmpty(String nfcMsg) {
        return mapDetailMapper.updateMapDetail(nfcMsg);
    }

    /**
     * 判断是否存在地图点位
     */
    @Override
    public boolean existMapDetailDesc(int mapId, String mapDetailDesc){
        return mapDetailMapper.getMapByMapDetailDesc(mapId, mapDetailDesc) != null;
    }

    /**
     * 查询所有已保存地图（无分页功能）
     */
    @Override
    public List<Map> getAllMaps(){
        return mapMapper.getAllMaps();
    }

    /**
     * 查看可添加点位
     */
    @Override
    public List<MapDetailVO> getExtraPoints(int mapId){
        List<MapDetailVO> allMapDetail = mapDetailMapper.getAllMapDetail(1);
        List<MapDetailVO>  oneMapDetail = mapDetailMapper.getAllMapDetail(mapId);
        List<MapDetailVO> filteredList = allMapDetail.stream()
                .filter(mapDetail -> oneMapDetail.stream()
                        .noneMatch(one -> one.getMapDetailDesc().equals(mapDetail.getMapDetailDesc())))
                .collect(Collectors.toList());

        return filteredList;
    }
}
